using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace OsdmSoftware.CommonLibrary
{
    /* ToolBand consists of several ToolPanels and IDropDownControls and manages their position
     */
    public class ToolBand: Panel
    {
        private List<Control> attachedList = new List<Control>();
        /*private Color foreColor;

        public Color ForeColor { get { return foreColor; } set { foreColor = value; } }*/

        public ToolBand() { }

        public void ControlsMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            CloseDropDowns();
        }

        public void CloseDropDowns()
        {
            foreach (Control c in Controls)
            {
                if (c is ToolPanel)
                {
                    ((ToolPanel) c).CloseDropDown();
                }
            }
        }

        /*protected override void OnPaint(PaintEventArgs e)
        {
            if (this.Dock == DockStyle.Left)
                e.Graphics.DrawLine(new Pen(foreColor), Width - 1, 0, Width - 1, Height - 1);
            else if (this.Dock == DockStyle.Right)
                e.Graphics.DrawLine(new Pen(foreColor), 0, 0, 0, Height - 1);
            else if (this.Dock == DockStyle.Top)
                e.Graphics.DrawLine(new Pen(foreColor), 0, Height - 1, Width - 1, Height - 1);
            else if (this.Dock == DockStyle.Bottom)
                e.Graphics.DrawLine(new Pen(foreColor), 0, 0, Width - 1, 0);

            base.OnPaint(e);
        }*/

        public void Attach(Control topControl)
        {
            if (topControl is ToolPanel)
                ((ToolPanel) topControl).BeforeMouseDown += ControlsMouseDown;
            else
                topControl.MouseDown += ControlsMouseDown;

            attachedList.Add(topControl);

            foreach (Control c in topControl.Controls)
            {
                Attach(c);
            }
        }

        public void Attach()
        {
            Detach();
            Attach(TopLevelControl);
        }

        public void Detach()
        {
            foreach (Control c in attachedList)
            {
                if (c is ToolPanel)
                    ((ToolPanel) c).BeforeMouseDown -= ControlsMouseDown;
                else
                    c.MouseDown -= ControlsMouseDown;
            }
        }

        public void LayoutPanels()
        {
#if !PocketPC
            {
                // This is a hack to force control to recompute its size.
                // All it takes is voodoo dance and smashing your head against the wall.
                IntPtr tempHandle = this.Handle;
                bool tempSHK = this.ShowFocusCues;
            }
#endif
            float area = 0;
            foreach (Control c in Controls)
            {
                if (c is ToolPanel)
                {
                    ToolPanel p = (ToolPanel) c;
                    p.AssignSelfToElements();
                    /*if (p.ShowAsTabs == ShowAsTabs.Left)
                        p.LayoutMethod = Orientation.Vertical;
                    else if (p.ShowAsTabs == ShowAsTabs.Top)
                        p.LayoutMethod = Orientation.Horizontal;
                    else
                    {*/
                        if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
                        {
                            p.LayoutMethod = Orientation.Vertical;
                        }
                        else
                        {
                            p.LayoutMethod = Orientation.Horizontal;
                        }
                    //}

                    area += p.Area;
                }
                else if (c is IDropDownControl)
                {
                    IDropDownControl dc = (IDropDownControl) c;
                    area += dc.Area;
                }
            }

            int oppSize = 0;
            int pos = 0;
            int size;
            if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
                size = Height - Controls.Count;
            else
                size = Width - Controls.Count;

            foreach (Control c in Controls)
            {
                if (c is ToolPanel)
                {
                    ToolPanel p = (ToolPanel) c;
                    if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
                    {
                        p.Location = new Point(0, pos);
                        p.Height = (int) Math.Max((size * p.Area + area / 2) / area, p.MinHeight);
                        pos += p.Height;
                        oppSize = Math.Max(oppSize, p.ElementLayout());
                    }
                    else
                    {
                        p.Location = new Point(pos, 0);
                        p.Width = (int) Math.Max((size * p.Area + area / 2) / area, p.MinWidth);
                        pos += p.Width;
                        oppSize = Math.Max(oppSize, p.ElementLayout());
                    }

                }
                else if (c is IDropDownControl)
                {
                    IDropDownControl dc = (IDropDownControl) c;
                    if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
                    {
                        dc.Control.Location = new Point(0, pos);
                        Size s = dc.GetOptimalSize(Int32.MaxValue, (int)((size * dc.Area + area / 2) / area));

                        dc.Control.Size = s;
                        pos += s.Height;

                        oppSize = Math.Max(oppSize, s.Width);
                    }
                    else
                    {
                        dc.Control.Location = new Point(pos, 0);
                        Size s = dc.GetOptimalSize((int)((size * dc.Area + area / 2) / area), Int32.MaxValue);
                        dc.Control.Size = s;
                        pos += s.Width;
                        oppSize = Math.Max(oppSize, s.Height);
                    }
                }
            }

            if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
            {
                Width = oppSize;
            }
            else
            {
                Height = oppSize;
            }

            foreach (Control c in Controls)
            {
                if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
                {
                    c.Width = oppSize;
                }
                else
                {
                    c.Height = oppSize;
                }

                c.Refresh();
            }
        }
    }
}
